package com.projet.whattowatch.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.projet.whattowatch.R;
import com.projet.whattowatch.adapter.GenreAdapterPlus;

public class CategoryFragment extends Fragment
{
	private ListView genreList;
	final String EXTRA_MOVIE = "movie_name";
	final String EXTRA_MOVIE_URL = "movie_url";

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View rootView = inflater.inflate(R.layout.category_layout, container, false);
		
		genreList = (ListView) rootView.findViewById(R.id.genreListView);
		
		String[] genreTab = getResources().getStringArray(R.array.list_genre);
		ArrayList<String> listGenre = new ArrayList<String>();
		for (int i = 0; i < genreTab.length; i++)
		{
			listGenre.add(genreTab[i]);
		}
		
		GenreAdapterPlus genreAdapter = new GenreAdapterPlus(getActivity(), listGenre);
		genreList.setAdapter(genreAdapter);
		
		genreList.setOnItemClickListener(new OnItemClickListener()
		{
		    @Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
		    { 
		    	String genre = genreList.getItemAtPosition(position).toString();
		    	
		    	ArrayList<String> movieName = new ArrayList<String>();
	            ArrayList<String> movieUrl = new ArrayList<String>();
	            
	            for(int i = 0; i < MainActivity.mLibFilm.getListFilm().size(); i++)
	            {
	            	ArrayList<String> genreList = MainActivity.mLibFilm.getFilm(i).getListGenre();
	            	for(int j = 0; j < genreList.size(); j++)
	            	{
	            		if(genreList.get(j).equals(genre))
	            		{
	            			movieName.add(MainActivity.mLibFilm.getFilm(i).getNom());
	            			movieUrl.add(MainActivity.mLibFilm.getFilm(i).getImageUrl());
	            		}
	            	}
	            }
		    	
		    	Intent listFilm = new Intent(getActivity().getApplicationContext(), PostGenreActivity.class);
		    	listFilm.putStringArrayListExtra(EXTRA_MOVIE, movieName);
		    	listFilm.putStringArrayListExtra(EXTRA_MOVIE_URL, movieUrl);
		    	startActivity(listFilm);
		    }
		 });

		return rootView;
	}
}
